<?php
/**
*
* 功能描述（购物车控制器）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class CtrlCart extends CtrlBase
{
	public $paramCheck = array(
		"add" => array(
			"id" => array("POST", "must", "intval", "positive"),
			"price" => array("POST", "must", "floatval", "notnegative"),
			"cnt" => array("POST", "must", "intval", "positive"),
			"autoPrice" => array("POST", "0", "intval", "notnegative"),
		),
		"remove" => array(
			"cart" => array("SESSION", "must", "", ""),
			"key" => array("GET", "must", "trim", "notempty"),
		),
		"changecnt" => array(
			"key" => array("GET", "must", "trim", "notempty"),
			"cart" => array("SESSION", "must", "", ""),
			"cnt" => array("GET", "must", "intval", "positive"),
		),
	);

	function Index()
	{
		$this->BindSession();
		if(array_key_exists('cart', $_SESSION))
			$cart = $_SESSION['cart'];
		else
			$cart = array();
		//未校验有效性
		$this->assign("cartlist", $cart);
		return RT_TPL;
	}

	function Add()
	{
		if($this->params['autoPrice'] == 0)
			$type = 0;
		else
			$type = 1;
		$id = $this->params['id'];
		$data= $this->GetItem($id, $type, $this->params['price'], $this->params['cnt']);
		if($data[0] == 0)
		{
			$k = $id .  "_" . $data[1]['size'];
			if(array_key_exists('cart', $_SESSION))
			{
				$cart = $_SESSION['cart'];
				if(array_key_exists($k, $cart))
					$data[1]['cnt'] += $cart[$k]['cnt'];
				$cart[$k] = $data[1];
			}
			else
				$cart = array($k => $data[1]);
			$_SESSION['cart'] = $cart;
		}
		echo $data[0];
		return RT_NONE;
	}

	function Remove()
	{
		$k = $this->params['key'];
		$cart = $_SESSION['cart'];
		if(array_key_exists($k, $cart))
			unset($_SESSION['cart'][$k]);
		jump_to("index.php?action=cart.index");
	}

	function ChangeCnt()
	{
		$cart = $_SESSION['cart'];
		$key = $this->params['key'];
		if(array_key_exists($key, $cart))
		{
			$obj = $_SESSION['cart'][$key];
			$cnt = $this->params['cnt'];
			$_SESSION['cart'][$key]['cnt'] = $cnt;
			return array("key" => $key, "ret" => 0, 'val' => $cnt, 'total' => round($obj['discount']*$obj['price'] * $cnt, 2));
		}
		else
			return array("key" => $key, "ret" => 1, 'val' => 'abc');
	}

	private function GetItem($id, $type, $param, $cnt)
	{
		$obj = $this->GetModel("Food");
		$data = $obj->GetGoods($id);
		if(!$data)
			return array(1, null);
		$item = array();
		$item['id'] = $data['id'];
		$item['name'] = $data['name'];
		$item['version'] = $data['version'];
		$item['status'] = 0;
		$disc = $data['discount'];
		$where = "false";
		switch($type)
		{
		case 0:
			$where = "goods_id = $id and round(price * $disc,2)  = $param";
			break;
		case 1:
			$where = "goods_id = $id and `default` = 1";
			break;
		case 2:
			$where = "goods_id = $id and id = $param";
			break;
		}
		$data = $obj->GetPrice($where);
		if(count($data)==0)
			return array(2, null);
		$data = $data[0];
		$item['size'] = $data['size'];
		$item['describe'] = $data['describe'];
		$item['price'] = $data['price'];
		$item['discount'] = $disc;
		$item['price_id'] = $data['id'];
		$item['cnt'] = $cnt;
		$item['unit'] = $data['unit'];
		return array(0, $item);
	}

	function Reload()
	{
		if(array_key_exists('cart', $_SESSION))
			$this->SELF_ReloadData();
		jump_to("index.php?action=cart.index");
	}

	function SELF_ReloadData()
	{
		$cart = $_SESSION['cart'];
		$newCart = array();
		foreach($cart as $item)
		{
			$data = $this->GetItem($item['id'], 2, $item['price_id'], $item['cnt']);
			switch($data[0])
			{
			case 1: $item['status'] = 2; break;
			case 2: $item['status'] = 3; break;
			case 0:
				$data = $data[1];
				if($data['version'] != $item['version'])
					$data['status'] = 1;
				$item = $data;
			}
			if($item['status'] > 1)
				$item['cnt'] = 0;
			$newCart[] = $item;
		}
		$_SESSION['cart'] = $newCart;
	}
}

